package b2.config
{
	public class Config
	{
		//基本信息
		public static var basicConfig:BasicConfig;
		//等级信息
		public static var levelConfigs:Array;
		//状态信息
		public static var statusConfigs:Object;
		//坦克合成信息
		public static var formulaConfigs:Object;
		//物品信息
		public static var objectConfigs:Object;
		//登陆奖励
		public static var loginGiftConfigs:Array;
		//子弹信息
		public static var bulletConfigs:Object;
		//影片信息
		public static var movieConfigs:Object;
		//效果信息
		public static var effectConfigs:Object;
		//任务信息
		public static var taskConfigs:Object;
		//要塞信息
		public static var fortressConfigs:Object;
		//战役信息
		public static var campConfigs:Object;
		//敌人信息
		public static var enemyConfigs:Object;
		//战场掉落物品信息
		public static var dropItemConfigs:Object;
		//关卡信息
		public static var battleConfigs:Object;
		
		//战役->要塞 索引表
		public static var campFortLink:Object;
		//任务排序 索引表（按关键字（任务类型，任务所需等级，任务唯一号）排序）
		public static var taskList:Array;
		
		public function Config()
		{
			
		}
		
		public static function parseXML(dataXML:XML):void
		{
			var i:int;
			//解析基本信息
			basicConfig = new BasicConfig(dataXML.basicInfo);
			//解析等级信息
			levelConfigs = new Array();
			for (i = 0; i < dataXML.levelInfo.level.length(); i++)
			{
				levelConfigs.push(new LevelConfig(dataXML.levelInfo.level[i]));
			}
			statusConfigs = new Object();
			for (i = 0; i < dataXML.statusInfo.status.length(); i++)
			{
				var statusConfig:StatusConfig = new StatusConfig(dataXML.statusInfo.status[i]);
				statusConfigs[statusConfig.index] = statusConfig;
			}
			//解析坦克合成信息
			formulaConfigs = new Object();
			for (i = 0; i < dataXML.mixtureTank.formula.length(); i++)
			{
				var formulaConfig:FormulaConfig = new FormulaConfig(dataXML.mixtureTank.formula[i]);
				formulaConfigs[formulaConfig.requireTanks[0]+"+"+formulaConfig.requireTanks[1]] = formulaConfig;
			}
			//解析坦克信息
			objectConfigs = new Object();
			for (i = 0; i < dataXML.tankInfo.tank.length(); i++)
			{
				var tankConfig:TankConfig = new TankConfig(dataXML.tankInfo.tank[i]);
				objectConfigs[tankConfig.index] = tankConfig;
			}
			//解析物品信息
			for (i = 0; i < dataXML.objectInfo.object.length(); i++)
			{
				var objectConfig:ObjectConfig = new ObjectConfig(dataXML.objectInfo.object[i]);
				objectConfigs[objectConfig.index] = objectConfig;
			}
			//解析装备信息
			for (i = 0; i < dataXML.equipInfo.equip.length(); i++)
			{
				var equipConfig:EquipConfig = new EquipConfig(dataXML.equipInfo.equip[i]);
				objectConfigs[equipConfig.index] = equipConfig;
			}
			//解析登陆奖励信息
			loginGiftConfigs = new Array();
			for (i = 0; i < dataXML.loginGiftInfo.loginGift.length(); i++)
			{
				loginGiftConfigs.push(new LoginGiftConfig(dataXML.loginGiftInfo.loginGift[i]));
			}
			//解析子弹信息
			bulletConfigs = new Object();
			for (i = 0; i < dataXML.bulletInfo.bullet.length(); i++)
			{
				var bulletConfig:BulletConfig = new BulletConfig(dataXML.bulletInfo.bullet[i]);
				bulletConfigs[bulletConfig.index] = bulletConfig;
			}
			//解析影片信息
			movieConfigs = new Object();
			for (i = 0; i < dataXML.movieInfo.movie.length(); i++)
			{
				var movieConfig:MovieConfig = new MovieConfig(dataXML.movieInfo.movie[i]);
				movieConfigs[movieConfig.index] = movieConfig;
			}
			//解析状态效果信息
			effectConfigs = new Object();
			for (i = 0; i < dataXML.effectInfo.effect.length(); i++)
			{
				var effectConfig:EffectConfig = new EffectConfig(dataXML.effectInfo.effect[i]);
				effectConfigs[effectConfig.index] = effectConfig;
			}
			//解析任务信息
			taskConfigs = new Object();
			taskList = new Array();
			for (i = 0; i < dataXML.taskInfo.task.length(); i++)
			{
				var taskConfig:TaskConfig = new TaskConfig(dataXML.taskInfo.task[i]);
				taskConfigs[taskConfig.index] = taskConfig;
				taskList.push(taskConfig.index);
			}
			taskList.sort(sortTask);
			//解析要塞信息
			fortressConfigs = new Object();
			campFortLink = new Object();
			for (i = 0; i < dataXML.fortressInfo.fortress.length(); i++)
			{
				var fortressConfig:FortressConfig = new FortressConfig(dataXML.fortressInfo.fortress[i]);
				fortressConfigs[fortressConfig.index] = fortressConfig;
				//战役->要塞 索引表
				for (var j:int = 0; j < fortressConfig.campIndex.length; j++)
					campFortLink[fortressConfig.campIndex[j]] = fortressConfig.index;
			}
			//解析战役信息
			campConfigs = new Object();
			for (i = 0; i < dataXML.campInfo.camp.length(); i++)
			{
				var campConfig:CampConfig = new CampConfig(dataXML.campInfo.camp[i]);
				campConfigs[campConfig.index] = campConfig;
			}
			//解析敌人信息
			enemyConfigs = new Object();
			for (i = 0; i < dataXML.enemyInfo.enemy.length(); i++)
			{
				var enemyConfig:EnemyConfig = new EnemyConfig(dataXML.enemyInfo.enemy[i]);
				enemyConfigs[enemyConfig.index] = enemyConfig;
			}
			//解析战场掉落物品信息
			dropItemConfigs = new Object();
			for (i = 0; i < dataXML.dropItemInfo.dropItem.length(); i++)
			{
				var dropItemConfig:DropItemConfig = new DropItemConfig(dataXML.dropItemInfo.dropItem[i]);
				dropItemConfigs[dropItemConfig.index] = dropItemConfig;
			}
			//解析关卡信息
			battleConfigs = new Object();
			for (i = 0; i < dataXML.battleInfo.battle.length(); i++)
			{
				var battleConfig:BattleConfig = new BattleConfig(dataXML.battleInfo.battle[i]);
				battleConfigs[battleConfig.index] = battleConfig;
			}
		}
		
		private static function sortTask(a:int, b:int):int
		{
			var taskConfigA:TaskConfig = taskConfigs[a];
			var taskConfigB:TaskConfig = taskConfigs[b];
			if (taskConfigA.type < taskConfigB.type)
				return -1;
			else if (taskConfigA.type > taskConfigB.type)
				return 1;
			else if (taskConfigA.requireLevel < taskConfigB.requireLevel)
				return -1;
			else if (taskConfigA.requireLevel > taskConfigB.requireLevel)
				return 1;
			else if (taskConfigA.index < taskConfigB.index)
				return -1;
			else if (taskConfigA.index > taskConfigB.index)
				return 1;
			else
				return 0;
		}
	}
}